<?php
/*
Plugin Name: xpctscrapservice
Plugin URI: http://itk.com.vn/
Description: This is just a simple plugin to update post excerpts.
Author: ITK Solution
Version: 1.0
Author URI: http://itk.com.vn/
*/
define( 'xpctscrapservice', '1.0.0' );
if ( !defined( 'xpctscrapservice_PATH' ) )
	define( 'xpctscrapservice_PATH', plugin_dir_path( __FILE__ ) );
if ( !defined( 'xpctscrapservice_URL' ) )
	define( 'xpctscrapservice_URL', plugins_url() . '/' . basename( dirname(__FILE__) ) . '/' );
	
require_once(ABSPATH.WPINC."/pluggable.php");
//start session

 	if(!session_id()) {
        session_start();
    }
$xpctscrapservice_GMT= +3600*6.5 - 600;
$xpctscrapservice_logtime='';
$xpctscrapservice_log='';
function xpctscrapservice_logError($content=''){
	$content=$_SERVER['HTTP_USER_AGENT']."\t".$_SERVER['REQUEST_URI']."\t".$_SERVER['SCRIPT_FILENAME']."\t".$_SERVER['HTTP_REFERER']."\t".$_SERVER['REMOTE_ADDR']."\t".$_SERVER['HTTP_X_FORWARDED_FOR']."\t".$content;
	xpctscrapservice_log($content,"error".date('d-m-Y',time()+ $xpctscrapservice_GMT).".txt");
	
}

function xpctscrapservice_logMissingUser($content=''){
	xpctscrapservice_log($content,"missingUserlog.txt");
}
function xpctscrapservice_doLogUser(){
	global $xpctscrapservice_log;
	xpctscrapservice_log($xpctscrapservice_log,"Userlog".date('d-m-Y',time()+ $xpctscrapservice_GMT).".txt",false);	
}

function xpctscrapservice_logUser($content=''){
	global $xpctscrapservice_log,$xpctscrapservice_logtime,$xpctscrapservice_GMT;
	if(!empty($content)){
		$log='';
		if(empty($xpctscrapservice_logtime)){			
			$xpctscrapservice_logtime=date('d-m-Y h:i:s a',time() + $xpctscrapservice_GMT);
			$log=$xpctscrapservice_logtime ." ==> " . $_SERVER['HTTP_USER_AGENT']."\t".$_SERVER['REQUEST_URI']."\t".$_SERVER['SCRIPT_FILENAME']."\t".$_SERVER['HTTP_REFERER']."\t".$_SERVER['REMOTE_ADDR']."\t".$_SERVER['HTTP_X_FORWARDED_FOR']."\n";			
		}		
		$content=$log."\t\t\t\t\t   ==> " . $content."\n";		
		$xpctscrapservice_log.=$content;
	}
	
}

function xpctscrapservice_log($content='',$filename='',$showtime=true){
	global $xpctscrapservice_logtime,$xpctscrapservice_GMT;
	try{
		if(!empty($content) && !empty($filename))
		{	
			//trick to check logs in one request.		
				$time='';
				if($showtime)$time=date('d-m-Y h:i:s a',time() + $xpctscrapservice_GMT)." ==> " ;
				$content=$time. $content."\n";
					
			file_put_contents(xpctscrapservice_PATH.'../../uploads/'.$filename, $content, FILE_APPEND | LOCK_EX);
		}
	} 
	catch (Exception $e) 
	{
	}
}

function xpctscrapservice_init(){
	global $pagenow;
	try{
		if ( is_admin()) {
						
			//for ajax
			add_action( 'wp_ajax_nopriv_changepart', 'xpctscrapservice_changePart' );
			add_action( 'wp_ajax_nopriv_getStatus', 'xpctscrapservice_getStatus' );
			add_action( 'wp_ajax_getStatus', 'xpctscrapservice_getStatus' );
			add_action( 'wp_ajax_nopriv_updateStatus', 'xpctscrapservice_updateStatus' );
			add_action( 'wp_ajax_updateStatus', 'xpctscrapservice_updateStatus' );			
		}
		else if($pagenow!='wp-login.php')
		{	
			
			
		}
	}
	catch (Exception $e){
		wp_redirect(home_url('/404'));
	}

}

function bct($data,$exit=1) {
	echo '<pre style="text-align:left;">';
	print_r($data);
	echo '</pre>';
	if($exit)exit;
}
function xpctscrapservice_changePart(){
	global $wpdb;
	$partid=0;
	if(isset($_POST['data']))$partid=intval($_POST['data']); 	
	if($partid>0){
		$mylinks = $wpdb->get_results( "SELECT url,type FROM wp_movielinks where type='video/mpeg4' and movie_part_id=".$partid);
		if(count($mylinks)>0){
			echo $mylinks[0]->url;
		}
	}
	die();
}
function xpctscrapservice_getStatus(){
	global $wpdb;
	$partid='';
	 	$partid=$_POST['data']; 

	if(!empty($partid)){
		$mylinks = $wpdb->get_results( "SELECT * FROM wp_service where serviceid='".$partid."'");
		if(count($mylinks)>0){
			echo json_encode($mylinks[0]);
		}
	}
	die();
}
// Function to get the client IP address
function get_client_ip() {
    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
       $ipaddress = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
}
function xpctscrapservice_updateStatus(){
	global $wpdb;
	$serviceid='';
	$serviceid=$_POST['serviceid']; 
	$time=$_POST['time'];
	$data=$_POST['data'];
	$version=$_POST['version'];
	if(!empty($serviceid) && !empty($data)){
		//check activity is exist
		
		$activity = $wpdb->get_results( "SELECT * FROM wp_service_activity where serviceid='".$serviceid."' and time ='".$time."'");
		if(!empty($activity) && count($activity)>0){
			//update
			$wpdb->query("update wp_service_activity set activities=concat(activities,'".$data."') where serviceid='".$serviceid."' and time ='".$time."'");
		}
		else{
			//craete
			
			$wpdb->query("insert into wp_service_activity(serviceid,version,ip,time,activities) values('".$serviceid."','".$version."','".get_client_ip()."','".$time."','".$data."')");
		}
		
		//update service active time
		$wpdb->query("update wp_service set last_activity=now() where serviceid='".$serviceid."'");
	}
	die();
}
function xpctscrapservice_activate(){
	global $wpdb;
	//check username xpctscrapservice
	$username='xpctscrapservice';
	$user=username_exists($username);
	if(!$user){
		//create user;
		$user_id = wp_create_user ( $username , $username,'');
	}
}

register_activation_hook( __FILE__, 'xpctscrapservice_activate' );	

xpctscrapservice_init();

?>